package io.kiki.stack.netty.channelManager;

import io.kiki.stack.netty.protocol.Protocol;
import io.kiki.stack.netty.protocol.ProtocolImpl;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * Heart beat triggerd.
 */
@Slf4j
@Setter
@Getter
@Sharable
public class HeartbeatHandler extends ChannelDuplexHandler {

    @Override
    public void userEventTriggered(final ChannelHandlerContext channelHandlerContext, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {

            Protocol protocol = ProtocolImpl.protocol;
            protocol.getHeartbeatTrigger().heartbeatTriggered(channelHandlerContext);
        } else {
            super.userEventTriggered(channelHandlerContext, evt);
        }
    }
}
